<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../../static/lib/layui-v2.6.3/css/layui.css" media="all">
	<link rel="stylesheet" href="../../../static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	<div class="layui-form-item">
		<h2 align="center">新增用户</h2>
	</div>
    <div class="layui-form-item">
        <label class="layui-form-label required">账号</label>
        <div class="layui-input-block">
            <input type="text" name="userID" lay-verify="required" lay-reqtext="账号不能为空" placeholder="请输入账号" value="" class="layui-input">
            <!-- <tip>填写自己管理账号的名称。</tip> -->
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
			<input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
<!--            <input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女"> -->
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
        </div>
    </div>
	
	<div class="layui-form-item layui-form-text">
	    <label class="layui-form-label required">角色</label>
	    <div class="layui-input-block">
			<select name="role" id="role" lay-verify="required" lay-reqtext="角色不能为空">
			</select>
	    </div>
	</div>
	
	<div class="layui-form-item">
	    <label class="layui-form-label">所属学校</label>
	    <div class="layui-input-block">
{#			<input type="text" name="department" placeholder="请输入所属学校" value="" class="layui-input">#}
	        <select name="department" id="school" lay-verify="required" lay-reqtext="学校不能为空">
			</select>
        </div>
	</div>
	<div class="layui-form-item">
	    <label class="layui-form-label">录入年级</label>
	    <div class="layui-input-block">
	        <input type="text" name="position" placeholder="限制只拥有录入该年级成绩的权限" value="" class="layui-input">
	    </div>
	</div>
	
    <div class="layui-form-item">
        <label class="layui-form-label">录入学科</label>
        <div class="layui-input-block">
            <input type="email" name="email" placeholder="限制只拥有录入该学科成绩的权限" value="" class="layui-input">
        </div>
    </div>
    
    
	
	<div class="layui-form-item layui-form-text">
	    <label class="layui-form-label">是否启用</label>
	    <div class="layui-input-block">
	        <input type="radio" name="enable" value="1" title="启用" checked="">
	        <input type="radio" name="enable" value="0" title="禁用">
	    </div>
	</div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
		
		// ajax 头部增加csrf_token
		var token = "{{ csrf_token }}"
		$.ajaxSetup({
		    headers: { 'X-CSRFTOKEN': `${token}` }, // 这里是headers，不是data,  CSRF
		});
		
		
		$(function(){   //js 加载时，AJAX请求
			  $.ajax({
				url:'/user-role-query',
				type:'post',
				dataType:'json',
				success:function(result){
					var list = result.data;
					var str = "<option value=''></option>"
					for (var i=0; i<list.length;i++) {
						str += "<option value=\""+list[i].roleID+"\">"+ list[i].roleName+"</option>"
					}
				    console.log(str);
					$('#role').html(str);
					form.render('select')   //重新渲染 form select内容
				},
				error:function(){
					layer.msg('部门查询API出错！');
				}
			})
            $.ajax({
				url:'/school-query',
				type:'post',
				dataType:'json',
				success:function(result){
					var list = result.data;
					var str = "<option value=''></option>"
					for (var i=0; i<list.length;i++) {
						str += "<option value=\""+list[i].schoolName+"\">"+ list[i].schoolName+"</option>"
					}
				    console.log(str);
					$('#school').html(str);
					form.render('select')   //重新渲染 form select内容
				},
				error:function(){
					layer.msg('部门查询API出错！');
				}
			})
		});
		
	
        //监听提交
        form.on('submit(saveBtn)', function (data) {
			
			//传给后台
			$.ajax({
				url:"user-add",
				type:"post",
				data:data.field,
				success:function(result){
					if (result.success){
						layer.msg(result.msg, { icon: 1, time: 2000 }, function () {
						  parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
						  parent.layui.table.reload('currentTableId');
						})
					}
					else{
						layer.alert(result.msg);
					}
					
				},
				error:function(result){
					alert("接口错误！！！");
				}
				});
			
			return false;
			
        });

    });
</script>



</body>
</html>